package bluetooth;

import java.io.IOException;
import javax.bluetooth.BluetoothStateException;
import javax.bluetooth.DeviceClass;
import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.DiscoveryListener;
import javax.bluetooth.LocalDevice;
import javax.bluetooth.RemoteDevice;
import javax.bluetooth.ServiceRecord;
import javax.bluetooth.UUID;
import javax.microedition.io.StreamConnection;

/**
 * @author pmp
 */

public class Client implements DiscoveryListener, BtListener
{
    private ClientListener clientListener;
	private UUID uuid;
	private int inquiryMode;
	private int connectionOptions;
	private BtVector deviceList;
    private DiscoveryAgent agent;
    private BtConnection btConnection;

    public void createClient(ClientListener _clientListener)
    {
      clientListener=_clientListener;
      agent= getAgent();
      uuid = new UUID(0x1101);
      inquiryMode = DiscoveryAgent.GIAC;
      connectionOptions = ServiceRecord.NOAUTHENTICATE_NOENCRYPT;
      deviceList = new BtVector();
      log("Client created");
    }

	public void startDeviceInquiry()
    {
		try
        {
			log("Start inquiry method - this will take few seconds...");
			agent.startInquiry(inquiryMode, this);
		}
        catch (Exception e)
        {
			log(e);
		}
	}

	public void deviceDiscovered(RemoteDevice btDevice, DeviceClass cod)
    {
        log("A device discovered (" + getDeviceInfo(btDevice) + ")");
        deviceList.add(btDevice);
	}

	public void inquiryCompleted(int discType)
    {
		log("Inquiry compeleted. Please select device from combo box.");
        clientListener.deviceListShown(deviceList);
	}

	public void startServiceSearch(RemoteDevice device)
    {
		try
        {
			log("Start search service from "+ getDeviceInfo(device));
			agent.searchServices(null, new UUID[] { uuid }, device, this);
		}
        catch (Exception e)
        {
			log(e);
		}
	}

	public void servicesDiscovered(int transId, ServiceRecord[] records)
    {
		log("Service discovered.");
		btConnection=new BtConnection(records[0].getConnectionURL(connectionOptions, false), this);
	}

    public void debugConnetion(StreamConnection _sC){
        btConnection=new BtConnection(_sC, this);
    }

	public void serviceSearchCompleted(int transID, int respCode)
    {
		log("Service search completed.");
        clientListener.connetionEstablished();
	}

    public void sendData(String data)
    {
        btConnection.sendData(data);
    }

    public void stoptServiceSearch()
    {
        agent.cancelInquiry(this);
    }

	private void log(String msg)
    {
        clientListener.statusUpdate(msg+"\n");
	}

	private void log(Throwable e)
    {
		log(e.getMessage());
	}

	private DiscoveryAgent getAgent()
    {
		try
        {
			return LocalDevice.getLocalDevice().getDiscoveryAgent();
		}
        catch (BluetoothStateException e)
        {
			throw new Error(e.getMessage());
		}
	}

    private String getDeviceInfo(RemoteDevice btDevice)
    {
        String s="<bez_nazwy>";
        try
        {
            s = btDevice.getFriendlyName(false) + " - " + btDevice.getBluetoothAddress();
        } 
        catch (IOException ex)
        {
            ex.printStackTrace();
        }
        return s;
    }

    public void connectionLost(String deviceAdress) {
        btConnection=null;
        clientListener.connectionLost(deviceAdress);
    }

    public void dataReceived(String data, String btAdress) {
        clientListener.dataReceived(data, btAdress);
    }
}

